<?php
namespace Admin\Controller;
use Common\Controller\Controller;
use lyf\Verify;
/**
 * 后台唯一不需要权限验证的控制器
 * @author jry <598821125@qq.com>
 */
class LoginController extends Controller
{
    /**
     * 后台登陆
     * @author jry <598821125@qq.com>
     */
    public function login()
    {
        if (empty($_COOKIE['TestCookie'])) {
            // echo "mei";die;
            $is_login = false;
        }else{
            // echo "you";die;
            $is_login = 1;
        }
        if (empty($_POST)) {
            // 登录
            if ($is_login) {
                $this->redirect('Admin/Index/index');
            }
            if (request()->isPost()) {
                $username = I('username');
                $password = I('password');
                // var_dump($username,$password);die;
                // 图片验证码校验
                // if (!$this->checkVerify(I('post.verify')) && 'localhost' !== request()->hostname() && '127.0.0.1' !== request()->hostname()) {
                //     $this->error('验证码输入错误！');
                // }
                // var_dump($this->checkVerify(I('post.verify')) && 'localhost' !== request()->hostname() && '127.0.0.1' !== request()->hostname());die;
                // 验证用户名密码是否正确
                $user_object = D('Admin/User');
                // var_dump($user_object);die;
                $user_info   = $user_object->login($username, $password);
                // var_dump($user_object->login($username, $password));die;
                // var_dump($user_info);die;
                if (!$user_info) {
                    $this->error($user_object->getError());
                }

                // 验证管理员表里是否有该用户
                $account_object = D('Admin/Access');
                $where['uid']   = $user_info['id'];
                $account_info   = $account_object->where($where)->find();
                if (!$account_info) {
                    $this->error('该用户没有管理员权限' . $account_object->getError());
                }

                // 设置登录状态
                $uid = $user_object->auto_login($user_info);

                // 跳转
                if (0 < $account_info['uid'] && $account_info['uid'] === $uid) {
                    $value = '100';
                    setcookie("TestCookie",$value, time()+3600);
                    $this->success('登录成功！', U('Admin/Index/index'));
                } else {
                    $this->logout();
                }
            } else {
                $this->assign('meta_title', '管理员登录');
                $this->display();
            }
        }else{
            // 登录
            if ($is_login) {
                $this->redirect('Admin/Index/index');
            }
            if ($_POST['yzm'] !== $_POST['num']) {
                $this->error('验证码输入有误！');
            }
            if (empty($_POST['yzm'])) {
                $this->error('验证码输入有误');
            }
            if (request()->isPost()) {
                $username = I('username');
                $password = I('password');
                // var_dump($username,$password);die;
                // 图片验证码校验
                // if (!$this->checkVerify(I('post.verify')) && 'localhost' !== request()->hostname() && '127.0.0.1' !== request()->hostname()) {
                //     $this->error('验证码输入错误！');
                // }
                // var_dump($this->checkVerify(I('post.verify')) && 'localhost' !== request()->hostname() && '127.0.0.1' !== request()->hostname());die;
                // 验证用户名密码是否正确
                $user_object = D('Admin/User');
                // var_dump($user_object);die;
                $user_info   = $user_object->login($username, $password);
                // var_dump($user_object->login($username, $password));die;
                // var_dump($user_info);die;
                if (!$user_info) {
                    $this->error($user_object->getError());
                }

                // 验证管理员表里是否有该用户
                $account_object = D('Admin/Access');
                $where['uid']   = $user_info['id'];
                $account_info   = $account_object->where($where)->find();
                if (!$account_info) {
                    $this->error('该用户没有管理员权限' . $account_object->getError());
                }

                // 设置登录状态
                $uid = $user_object->auto_login($user_info);

                // 跳转
                if (0 < $account_info['uid'] && $account_info['uid'] === $uid) {
                    $value = '100';
                    setcookie("TestCookie",$value, time()+3600);
                    $this->success('登录成功！', U('Admin/Index/index'));
                } else {
                    $this->logout();
                }
            } else {
                $this->assign('meta_title', '管理员登录');
                $this->display();
            }
        }
    }

    /**
     * 注销
     * @author jry <598821125@qq.com>
     */
    public function logout()
    {
        session('user_auth', null);
        session('user_auth_sign', null);
        setcookie("TestCookie",$value, time()-1);
        $this->success('退出成功！', U('login'));
    }

    /**
     * 图片验证码生成，用于登录和注册
     * @author jry <598821125@qq.com>
     */
    public function verify($vid = 1)
    {
        $verify         = new Verify();
        $verify->length = 4;
        $verify->entry($vid);
    }

    /**
     * 检测验证码
     * @param  integer $id 验证码ID
     * @return boolean 检测结果
     */
    protected function checkVerify($code, $vid = 1)
    {
        $verify = new Verify();
        return $verify->check($code, $vid);
    }
}
